package com.newzqxq.koalamoney.ui.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;

import com.newzqxq.koalamoney.R;
import com.newzqxq.koalamoney.common.BaseActivity;
import com.newzqxq.koalamoney.common.Params;
import com.newzqxq.koalamoney.utils.AppUtils;
import com.newzqxq.koalamoney.utils.BitmapUtil;
import com.newzqxq.koalamoney.utils.PawdCache;
import com.newzqxq.koalamoney.utils.StatusBarUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Sym on 2016/1/18.
 */
public class SplashActivity extends BaseActivity {

    @BindView(R.id.splash_iv)
    ImageView splashIv;

    private PawdCache aCache;
    private Bitmap splashBitmap;
    private int screenWidth, screenHeight;
    private SharedPreferences sp;
    private Handler handler = new Handler() {
    };
    private static final String TAG = "SplashActivity";


    @Override
    protected int getContentViewId() {
        return R.layout.ui_splash;
    }

    @Override
    protected void initData() {
        StatusBarUtil.transparencyBar(this);
    }

    @Override
    protected void initView() {
        int net_type = AppUtils.getConnectedType(mContext);
        Log.i("result", "clearMemory: " + net_type);
        init();
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void clearMemory() {


    }

    private void init() {
        aCache = PawdCache.get(this);
        sp = getSharedPreferences(Params.CONFIG, MODE_PRIVATE);
        screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        screenHeight = getWindowManager().getDefaultDisplay().getHeight();
        splashBitmap = BitmapUtil.resizeBitmap(screenWidth, screenHeight - getStatusBarHeight(),
                BitmapFactory.decodeResource(getResources(), R.mipmap.icon_splash));
        splashIv.setImageBitmap(splashBitmap);
        this.doJump();
    }

    private void doJump() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                String phone = sp.getString(Params.PHONE, "");
                String pawd = sp.getString(Params.PAWD, "");
                if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(pawd)) {//没有登录注册
                    Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    String gesturePassword = aCache.getAsString(Params.GESTURE_PASSWORD);
                    if (gesturePassword == null || "".equals(gesturePassword)) {
                        Intent intent = new Intent(SplashActivity.this, CreateGestureActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        Intent intent = new Intent(SplashActivity.this, GestureLoginActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
            }
        }, 2000);
    }

    private int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

}
